package com.bluesky.drcp.business.service;

import com.bluesky.drcp.business.ClientConfig;
import com.bluesky.drcp.persistence.dao.ClientConfigDao;

public class ClientConfigService {
	private ClientConfigDao clientConfigDao;

	public ClientConfigDao getClientConfigDao() {
		return clientConfigDao;
	}

	public void setClientConfigDao(ClientConfigDao clientConfigDao) {
		this.clientConfigDao = clientConfigDao;
	}

	public String getContentById(String id) {
		ClientConfig cc = clientConfigDao.getById(id);
		if(cc==null)
			return null;
		else
			return cc.getContent();
	}

	public String getContent(String host, String module) {

		String id = clientConfigDao.getId(host, module);
		if(id==null)
			return null;
		else
			return getContentById(id);

	}
	
	public void delete(String id)
	{
		clientConfigDao.deleteById(id);
	}

	public void setContent(String host, String module, String content) {
		String id;

		id = clientConfigDao.getId(host, module);
		if(id==null)
			id="";
		ClientConfig cc = clientConfigDao.getById(id);

		if(cc != null)
		{
			cc.setContent(content);
			clientConfigDao.update(cc);
		}
		else
		{
			cc = new ClientConfig();
			//cc.Id is uuid
			cc.setHost(host);
			cc.setModule(module);
			cc.setContent(content);
						
			clientConfigDao.insert(cc);
		}
	}
}
